package cc.autoapi.pucong.auto.flow.core.root.properties;

import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

public class AutoFlowDesignConfiguration {

    /**
     * 是否装配liteflow design
     */
    private Boolean enable = true;

    /**
     * web页面入口
     */
    private String web = "/auto-flow-web";


    /**
     * 是否启用跨域支持
     *
     * @since 1.0.0
     */
    private boolean supportCrossDomain = true;

    private JdbcConfig jdbcConfig = new JdbcConfig();

    private SecurityConfig securityConfig = new SecurityConfig();

    public boolean isSupportCrossDomain() {
        return supportCrossDomain;
    }

    public void setSupportCrossDomain(boolean supportCrossDomain) {
        this.supportCrossDomain = supportCrossDomain;
    }

    public String getWeb() {
        return web;
    }

    public String getWebPath() {
        if (StringUtils.isBlank(web)) {
            return null;
        }
        if (!web.startsWith("/")) {
            return "/" + web;
        }
        if (web.endsWith("/**")) {
            return web.substring(0, web.length() - 3);
        }
        if (web.endsWith("/*")) {
            return web.substring(0, web.length() - 2);
        }
        if (web.endsWith("/")) {
            return web.substring(0, web.length() - 1);
        }
        return web;
    }

    public void setWeb(String web) {
        this.web = web;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public JdbcConfig getJdbcConfig() {
        return jdbcConfig;
    }

    public void setJdbcConfig(JdbcConfig jdbcConfig) {
        this.jdbcConfig = jdbcConfig;
    }

    public SecurityConfig getSecurityConfig() {
        return securityConfig;
    }

    public void setSecurityConfig(SecurityConfig securityConfig) {
        this.securityConfig = securityConfig;
    }

    /**
     * 打印banner
     */
    public void printBanner(String systemPath, Integer systemPort) {
        System.out.println("                  _           __ _              ");
        System.out.println("                 | |         / _| |             ");
        System.out.println("       __ _ _   _| |_ ___   | |_| | _____      __     __");
        System.out.println("      / _` | | | | __/ _ \\  |  _| |/ _ \\ \\ /\\ / /  /");
        System.out.println("     | (_| | |_| | || (_) | | | | | (_) \\ V  V / V   /");
        System.out.println(String.format("      \\__,_|\\__,_|\\__\\___/  |_| |_|\\___/ \\_/\\_/ %s, %s", getVersion(), getUrl(systemPath, systemPort)));
    }

    private String getUrl(String systemPath, Integer systemPort) {
        return String.format("http://127.0.0.1:%d%s%s/index.html", systemPort, systemPath, getWebPath());
    }

    private String getVersion() {
        return Optional.ofNullable(AutoFlowDesignConfiguration.class.getPackage()).map(Package::getImplementationVersion).orElse("DEV");
    }
}
